import { get } from './index'
import { QUESTION_TYPE_DESCRIPTION, QUESTION_TYPE_MATRIX_RADIO, DRAW_SCHEMA, COLLECT_SCHEMA } from '@/constants/default'
import { getPath } from '@/utils/utils'

function formatData(data) {
  let questions = data.questions
  let questionsTitle = []

  questions.forEach(q => {
    if (q.type === QUESTION_TYPE_DESCRIPTION) return
    // if (q.type === QUESTION_TYPE_MATRIX_RADIO) {
    //   return
    // }
    questionsTitle.push(q.title)
  })

  return questionsTitle
}

export default function () {
  return new Promise(async (res, rej) => {
    // 错误类型: 向后台请求, 但是后台报错(查询无结果), 此时提示 FAIL; 本地前端发送报错(超时), 此时响应 ERROR
    try {
      const resData = await get(`/api/survey/${getPath()}`)
      if (resData.data.code === 200) {
        res(formatData(resData.data.data))
        return
      }
      console.error(resData)
      rej('FAIL：' + resData.data.msg)
    } catch (error) {
      console.error(error)
      rej('ERROR：' + error.message)
    }
  })
}
